package lyh.hnu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import lyh.hnu.common.CustomException;
import lyh.hnu.common.R;
import lyh.hnu.dto.SetmealDto;
import lyh.hnu.entity.po.Setmeal;
import lyh.hnu.entity.po.SetmealDish;
import lyh.hnu.mapper.SetmealMapper;
import lyh.hnu.service.SetmealDishService;
import lyh.hnu.service.SetmealService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

@Service
@Slf4j
public class SetmealServiceImpl extends ServiceImpl<SetmealMapper, Setmeal> implements SetmealService {
    @Autowired
    private SetmealDishService setmealDishService;

    @Transactional
    @Override
    public R<String> saveWithDish(SetmealDto setmealDto) {
        save(setmealDto);
        Long setmealId = setmealDto.getId();
        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
        setmealDishes = setmealDishes.stream().map(item -> {
           item.setSetmealId(setmealId);
           return item;
        }).collect(Collectors.toList());
        setmealDishService.saveBatch(setmealDishes);
        return R.success("新增套餐成功");
    }

    @Transactional
    @Override
    public R<String> removeWithDish(List<Long> ids) {
        //只能删除停售的套餐
        Long count = lambdaQuery().in(Setmeal::getId, ids).eq(Setmeal::getStatus, 1).count();
        if (count > 0) {
            throw new CustomException("正在售卖中的套餐无法删除");
        }
        removeByIds(ids);
        List<SetmealDish> setmealDishList = setmealDishService.lambdaQuery()
                .in(SetmealDish::getSetmealId, ids)
                .list();
        setmealDishService.removeByIds(setmealDishList);
        return R.success("删除套餐成功");
    }

    @Override
    public R<SetmealDto> selectWithDish(long id) {
        Setmeal setmeal = getById(id);
        SetmealDto setmealDto = new SetmealDto();
        BeanUtils.copyProperties(setmeal, setmealDto);
        List<SetmealDish> dishList = setmealDishService.lambdaQuery().eq(SetmealDish::getSetmealId, id).list();
        setmealDto.setSetmealDishes(dishList);
        return R.success(setmealDto);
    }

    @Transactional
    @Override
    public R<String> updateWithDish(SetmealDto setmealDto) {
        updateById(setmealDto);
        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
        LambdaQueryWrapper<SetmealDish> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SetmealDish::getSetmealId, setmealDto.getId());
        setmealDishService.remove(queryWrapper);
        setmealDishes = setmealDishes.stream().map(item -> {
            item.setSetmealId(setmealDto.getId());
            return item;
        }).collect(Collectors.toList());
        setmealDishService.saveBatch(setmealDishes);
        return R.success("修改套餐成功");
    }


}
